home *** CD-ROM | disk | FTP | other *** search
- Path: liberty.b23a.ingr.com!dpmikese
- From: dpmikese@ingr.com (Dave Mikesell)
- Newsgroups: comp.lang.c
- Subject: Re: Dangling pointer?
- Date: Fri, 19 Apr 1996 12:56:19
- Organization: Intergraph
- Message-ID: <dpmikese.15.008B3840@ingr.com>
- References: <4l0r4b$jte@dewey.csun.edu> <4l48n6$lj8@texas.nwlink.com>
- NNTP-Posting-Host: liberty.b23a.ingr.com
- X-Newsreader: Trumpet for Windows [Version 1.0 Rev B final beta #4]
-
- In article <4l48n6$lj8@texas.nwlink.com> Teresa Reiko <tjr19@mail.nwlink.com> writes:
-
- >kc44097@csun.edu (chen) wrote:
- >>
- >> What is "dangling pointer",can someone give me a defination and example?
- >>Please e-mail me kc44097@huey.csun.edu
-
- >A dangling pointer is, usually, a pointer that pointed to some
- >memory allocated with malloc() or calloc() that was then freed.
-
- >Dangling pointers cause trouble if the memory they pointed
- >to is accessed, since it usually gets re-used.
-
- >Example:
-
- >char *p;
-
- >p = malloc(100);
-
- >..
-
- >free(p);
-
- >/* now p is a dangling pointer */
-
- >*p = 0; /* you can't do this */
-
- Actually (and unfortunately) you can do this. But you shouldn't,
- which I'm sure is what Teresa was trying to say. But the fact that you can
- is the reason it is dangerous - if you assign *p a value you need later, it
- may or may not be there when you need it, depending on whether or not its
- space on the heap was given to another pointer - after all, it was given back
- to the system when free(p) was called.
-
- --
- Dave M.
-